<template>
    <div>
        <div style="height:30px;margin-top:3px" v-if="!disabledForStop">
            <Button type="primary" @click="createSs11002()" :disabled="familyInfo.isSendedHd||familyInfo.townshipEdit"
                    icon="md-add">保障人员录入
            </Button>
            <Button type="primary" class="step-item" style="margin-left:5px" @click="createSs11012()"
                    :disabled="familyInfo.isSendedHd||familyInfo.townshipEdit"
                    icon="md-add">非保障人员录入
            </Button>
        </div>
        <ss17002-modal ref="ss17002Modal" @ok="reloadData"></ss17002-modal>
        <ss17012-modal ref="ss17012Modal" @ok="reloadData"></ss17012-modal>
        <div style="margin-top: 10px">
            <Table :columns="columns" :data="tableData" :loading="Loading" border>
                <template slot-scope="{ row, index}" slot="action">
                    <Button type="info" size="small" style="margin-right: 5px" @click="handleEdit(index)"
                            :disabled="disabledForStop" v-if="!disabledForStop">编辑
                    </Button>
                    <Button type="error" size="small" style="margin-right: 5px" @click="handleDelete(index)"
                            :disabled="familyInfo.isSendedHd||familyInfo.townshipEdit" v-if="!disabledForStop">删除
                    </Button>
                    <Button type="success" size="small" style="margin-right: 5px" @click="handleView(index)">详情</Button>
                    <Button v-if="row.azcp0517!='01' && row.type_name!='非保障人员' && !disabledForStop"
                            type="warning" size="small" style="margin-right: 5px" @click="changeToHost(index)" :disabled="familyInfo.isSendedHd||familyInfo.townshipEdit">设为户主
                    </Button>
                </template>
            </Table>
            <!--
            <vxe-table border
                       row-id="id"
                       :row-key="true"
                       show-overflow="tooltip"
                       highlight-hover-row
                       align="center"
                       header-align="center"
                       ref="xTree"
                       :loading="Loading"
                       :data="tableData">
                <vxe-table-column field="azcp0003" title="姓名" align="center"  />
                <vxe-table-column field="azcp0001" title="证件号码" align="center" />
                <vxe-table-column field="azcp0004_name" title="性别" align="center" />
                <vxe-table-column field="azcp0517_name" title="与申请人关系" align="center" />
                <vxe-table-column field="azcp0007_name" title="民族" align="center"/>
                <vxe-table-column field="azcp0009_name" title="健康状况" align="center"/>
                <vxe-table-column field="azcp0502_name" title="劳动能力丧失情况" align="center"/>
                <vxe-table-column field="type_name" title="保障状态" align="center" />
                <vxe-table-column field="" title="操作" min-width="100" fixed="right">
                    <template v-slot="{ row, index }">
                        <Button type="info" size="small" style="margin-right: 5px" @click="handleEdit(index)">编辑</Button>
                        <Button type="info" size="small" style="margin-right: 5px" @click="handleDelete(index)">删除</Button>
                        <Button type="success" size="small" style="margin-right: 5px" @click="handleView(index)">详情</Button>
                    </template>
                </vxe-table-column>
            </vxe-table>-->
            <div style="margin: 10px;overflow: hidden">
                <div style="float: right;">
                    <Page :total="dataCount" :page-size="pageSize" show-total class="paging"
                          @on-change="handleTableChange" :current="pageNo"></Page>
                </div>
            </div>
        </div>
        <div style="height:40px;line-height:40px;margin-top:10px;text-align: center ">
            <Button @click="prevStep">上一步</Button>
            <Button type="primary" @click="nextStep" :loading="btnLoading" style="margin-left:20px">下一步
            </Button>
        </div>
    </div>
</template>

<script>
    import ss17002Modal from './modules/Ss17002Modal.vue'
    import ss17012Modal from './modules/Ss17012Modal.vue'
    import {postAction} from '@/api/request'
    import {CommonMixin} from '@/views/common/js/CommonMixin'

    export default {
        name: 'application2',
        mixins: [CommonMixin],
        components: {
            ss17002Modal, ss17012Modal
        },
        data() {
            return {
                url: {
                    list: '/api/ac/subsistence/ss17002Service/getPageList',
                    getSs17002One: '/api/ac/subsistence/ss17002Service/getOne',
                    getSs17012One: '/api/ac/subsistence/ss17012Service/getOne',
                    deleteSs17002: '/api/ac/subsistence/ss17002Service/deleteData',
                    deleteSs17012: '/api/ac/subsistence/ss17012Service/deleteData',
                    checkStep2: '/api/ac/subsistence/ss17002Service/checkPersonInfoSs17',
                },
                dataCount: 0,
                pageSize: 10,
                familyInfo: {},
                queryParam: {},
                columns: [
                    {
                        title: '姓名',
                        key: 'azcp0003',
                        align: 'center',
                        width: 150
                    },
                    {
                        title: '与申请人关系',
                        key: 'azcp0517_name',
                        align: 'center',
                        width: 150
                    },
                    {
                        title: '性别',
                        key: 'azcp0004_name',
                        align: 'center',
                        width: 120
                    },
                    {
                        title: '证件号码',
                        key: 'azcp0001',
                        align: 'center',
                        width: 200
                    },
                    {
                        title: '民族',
                        key: 'azcp0007_name',
                        align: 'center',
                        width: 100
                    },
                    {
                        title: '健康状况',
                        key: 'azcp0009_name',
                        align: 'center',
                        width: 150
                    },
                    {
                        title: '劳动能力丧失情况',
                        key: 'azcp0502_name',
                        align: 'center',
                        width: 200
                    },
                    {
                        title: '查验状态',
                        key: 'azcp0578_name',
                        align: 'center',
                        width: 150
                    },
                    {
                        title: '保障状态',
                        key: 'type_name',
                        align: 'center',
                        width: 200
                    },
                    {
                        title: '操作',
                        slot: 'action',
                        align: 'center',
                        fixed: 'right',
                        width: 270
                    }
                ],
                tableData: [],
                Loading: false,
                disabledForStop: false,
                btnLoading: false,
                ss17001Id: this.$parent.familyInfo.ss17001Id,
                ss17002Id: this.$parent.familyInfo.ss17002Id,
            }
        },
        methods: {
            prevStep() {
                this.$emit('prevStep')
            },
            nextStep() {
                let that = this;
                that.btnLoading = true
                let url = that.url.checkStep2
                let dataParam = {};
                dataParam.ssca0087 = that.familyInfo.ssca0087;
                dataParam.azcp0500 = that.ss17001Id;
                dataParam.ssca0016 = that.familyInfo.ssca0016;
                dataParam.ssca0002 = that.familyInfo.ssca0002;
                dataParam.ss12001Id = that.familyInfo.ss12001Id;
                let yjab0012 = that.familyInfo.yjab0012;
                if(yjab0012=='01'||yjab0012=='02'||yjab0012=='04'){
                    //预警处置 已处置、维持现状不校验
                    this.$emit('nextStep')
                }else
                if (dataParam.ssca0016 != '300' && dataParam.ssca0016 != '500' && dataParam.ssca0016 != '501') {
                    postAction(url, dataParam).then((res) => {
                      if (res.data.errcode == 9999) {
                        this.$Message.error(res.data.errmsg);
                      } else {
                        if (res.data.data.code == 1) {
                          this.$emit('nextStep')
                        } else {
                          that.$Message.error(res.data.data.msg);
                        }
                      }
                    }).finally(() => {
                        that.btnLoading = false
                    });
                } else {
                    //停救、区内迁移和跨区迁移，不校验checkPersonInfoSs17
                    this.$emit('nextStep');
                }
            },
            handleEdit(index) {
                let that = this;
                let record = that.tableData[index];
                let url = null;
                let dataParam = {};
                dataParam.id = record.id;
                if (record.id) {
                    if (record.type == '1') {
                        url = that.url.getSs17002One
                        that.$refs.ss17002Modal.load(url, dataParam, that.familyInfo);
                    } else {
                        url = that.url.getSs17012One
                        that.$refs.ss17012Modal.load(url, dataParam);
                    }
                } else {
                    that.$Message.error('请选择操作数据');
                }
            },
            createSs11002() { // 录入保障人员
                let that = this;
                that.$refs.ss17002Modal.ss17001Id = that.$parent.familyInfo.ss17001Id
                that.$refs.ss17002Modal.ss12001Id = that.$parent.familyInfo.ss12001Id
                that.$refs.ss17002Modal.create()
            },
            createSs11012() { // 录入非保障人员
                let that = this;
                that.$refs.ss17012Modal.ss17001Id = that.ss17001Id
                that.$refs.ss17012Modal.create()
            },
            handleDelete(index) {
                let that = this;
                let record = that.tableData[index];
                if (record.id) {
                    let isMaster = record.azcp0517;
                    if (isMaster == '01') {
                        that.$Message.error('家庭户主不可删除，如需删除请更新家庭户主再操作');
                        return
                    } else if (record.azcp0001 == that.familyInfo.ssca0083) {
                        that.$Message.error('当前人员为本户保障金发放的开户人, 若要删除此人, 请先通过银行账户信息维护功能更改开户人');
                        return
                    } else {
                        this.$Modal.confirm({
                            title: '确定删除吗',
                            onOk: () => {
                                let url = null;
                                let dataParam = {};
                                dataParam.id = record.id;
                                dataParam.azcp0001 = record.azcp0001;
                                dataParam.azcp0003 = record.azcp0003;
                                dataParam.azcp0500 = record.azcp0500;
                                if (record.type == '1') {
                                    url = that.url.deleteSs17002
                                } else {
                                    url = that.url.deleteSs17012
                                }
                                postAction(url, dataParam).then((res) => {
                                    if (res.data.data.code == 1) {
                                        that.$Message.success(res.data.data.msg);
                                        that.reloadData();
                                    } else {
                                        that.$Message.error(res.data.data.msg);
                                    }
                                });
                            },
                            onCancel: () => {
                            }
                        });
                    }

                } else {
                    that.$Message.error('请选择操作数据');
                }
            },
            changeToHost(index) {
                let that = this;
                let record = that.tableData[index];
                if (record.id) {
                    this.$Modal.confirm({
                        title: '确定变更为申请人',
                        onOk: () => {
                            postAction('/api/ac/subsistence/ss17002Service/updateAndChangeToHost', record).then((res) => {
                                that.loadData();
                            });
                        },
                        onCancel: () => {
                        }
                    });
                } else {
                    that.$Message.error('请选择操作数据');
                }
            },
            handleView(index) {
                console.log("aaaaaaaaa")
                let that = this;
                let record = that.tableData[index];
                let url = null;
                let dataParam = {};
                dataParam.id = record.id;
                if (record.id) {
                    if (record.type == '1') {
                        url = that.url.getSs17002One
                        postAction(url, dataParam).then((res) => {
                            if (res.data.data.code == 1) {
                                res.data.data.data.ss12001Id = that.familyInfo.ss12001Id
                                console.log(res)
                                    that.$refs.ss17002Modal.view(res.data.data.data)
                            } else {
                                that.$Message.error(res.data.data.msg);
                            }
                        });
                    } else {
                        url = that.url.getSs17012One
                        postAction(url, dataParam).then((res) => {
                            if (res.data.data.code == 1) {
                                that.$refs.ss17012Modal.view(res.data.data.data)
                            } else {
                                that.$Message.error(res.data.data.msg);
                            }
                        });
                    }
                } else {
                    that.$Message.error('请选择操作数据');
                }
            },
            reloadData() {
                let that = this;
                that.queryParam.azcp0500 = that.ss17001Id;
                that.loadData();
            },
        },
        created() {
            let that = this;
            that.familyInfo = that.$parent.familyInfo;
            that.queryParam.azcp0500 = that.familyInfo.ss17001Id;
            //that.queryParam.azcp0500='ab77b34f1ecb4aa28b0e2b95c0db868d';
            that.loadData();
            //如是停救，operationType=300,则页面不可编辑
            if (that.$parent.familyInfo.ssca0016 == '300' || that.$parent.familyInfo.ssca0016 == '500' || that.$parent.familyInfo.ssca0016 == '501') {
                that.disabledForStop = true
            }else if(that.$parent.familyInfo.yjab0012=='01'||that.$parent.familyInfo.yjab0012=='02' ||that.$parent.familyInfo.yjab0012=='04'){
              //预警处置 已处置、维持现状 不可编辑数据
              that.disabledForStop = true
            }
        },
    }
</script>

<style scoped>

</style>
